package com.forchange.exe1

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author chencong
 * @Description 实现累加器
 * @Date 10:00 下午 2021/6/26
 * @Param
 * */
object AccumulatorTest {
  def main(args: Array[String]): Unit = {
    val list = List(1, 2, 3, 4, 5)
    //1 创建spark的环境
    val sparkConf = new SparkConf().setMaster("local").setAppName("AccumulatorTest")
    //2 连接spark
    val scc = new SparkContext(sparkConf)
    //3 逻辑
    val rdd: RDD[Int] = scc.makeRDD(list, 2)
    //3.1 创建累加器 sum的初始值是10
    val accumulator = new Accumulator
    //3.2 注册
    scc.register(accumulator,"CCsum")
    //3.3 调用add方法方累加
    rdd.foreach(x=>accumulator.add(x))
    //3.4 查看结果value
    println(accumulator.value)
    //4 关闭资源
    scc.stop()
  }

}
